/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;

/**
 * ConfigUtil defines operations to find the path of a containing file
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ConfigUtil {

	private static ConfigUtil singleton = new ConfigUtil();
	public static final String WEB_INF = "WEB-INF";
	public static final String BIN = "bin";
	public static final String JAR = "jar";
	public static final String UTF8 = "utf-8";
	public static final String FILE = "file";
	public static final String CLASS_POSTFIX = ".class";
	public static final String JAR_POSTFIX = ".jar!/";
	public static final String FILE_URL_PREFIX = "file:///";
	public static final String JAR_FILE_URL_PREFIX = "jar:file:";

	private ConfigUtil() {
	}

	public static ConfigUtil getInstance() {
		return singleton;
	}

	/**
	 * Read the root path of the web application
	 * 
	 * @return root path of web application
	 */
	public static String getReadRootPath() {
		String result = ConfigUtil.class.getResource(
				ConfigUtil.class.getSimpleName() + CLASS_POSTFIX).toString();

		int index = result.indexOf(WEB_INF);
		if (index == -1) {
			index = result.indexOf(BIN);
		}
		if (index != -1)

			result = result.substring(0, index);
		if (result.startsWith(JAR)) {

			int flag = result.indexOf(JAR_POSTFIX);
			if (flag != -1)
				result = result.substring(0, flag + 5);

		} else if (result.startsWith(FILE)) {

			result = result.substring(6);
		}
		if (result.endsWith(Symbols.SLASH))
			result = result.substring(0, result.length() - 1);
		try {
			result = URLDecoder.decode(result, UTF8);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	/**
	 * Get the root path of the web application
	 * 
	 * @return root path
	 */
	public static String getWriteRootPath() {
		String result = ConfigUtil.class.getResource(
				ConfigUtil.class.getSimpleName() + CLASS_POSTFIX).toString();

		int index = result.indexOf(WEB_INF);
		if (index == -1) {
			index = result.indexOf(BIN);
		}
		if (index != -1)

			result = result.substring(0, index);
		if (result.startsWith(JAR)) {
			result = result.substring(10);
			int flag = result.indexOf(JAR_POSTFIX);
			if (flag != -1)
				result = result.substring(0, flag);

			result = result.substring(0, result.lastIndexOf(Symbols.SLASH));
		} else if (result.startsWith(FILE)) {

			result = result.substring(6);
		}
		if (result.endsWith(Symbols.SLASH))
			result = result.substring(0, result.length() - 1);
		try {
			result = URLDecoder.decode(result, UTF8);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	/**
	 * Get URL instance from URL string
	 * 
	 * @param urlString
	 *            a string of file's URL
	 * @return URL instance
	 */
	public static URL transform(String urlString) {
		if (urlString == null)
			return null;
		if (urlString.startsWith(JAR_FILE_URL_PREFIX))
			urlString = urlString.substring(0,
					urlString.indexOf(Symbols.EXCLAMATION_MARK))
					+ urlString.substring(
							urlString.indexOf(Symbols.EXCLAMATION_MARK))
							.replace(Symbols.DOUBLE_SLASH, Symbols.SLASH);
		if (!urlString.startsWith(JAR_FILE_URL_PREFIX)
				&& !urlString.startsWith(FILE_URL_PREFIX)) {
			urlString = FILE_URL_PREFIX + urlString;
		}

		try {
			URL url = new URL(urlString);
			return url;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Get input stream from the file path
	 * 
	 * @param filePath
	 *            file path
	 * @return InputStream instance
	 */
	public static InputStream getInputStream(String filePath) {

		URL url = transform(filePath);
		if (url != null)
			try {
				return url.openStream();
			} catch (Exception e) {
				e.printStackTrace();
			}

		return null;
	}

}
